local jichong = fk.CreateSkill{
  name = "mini__jichong",
  dynamic_desc = function(self, player)
    return player:getMark("_mini__jichong") < 3 and
        "mini__jichong_inner:"..(2 * (player:getMark("_mini__jichong_next") + 1))..
        ":"..(1 + player:getMark("_mini__jichong_next"))..
        ":"..(3 - player:getMark("_mini__jichong"))
        or "mini__jichong_fin"
  end,
}

Fk:loadTranslationTable{
  ["mini__jichong"] = "积宠",
  [":mini__jichong"] = "当你受到伤害或回复体力后，你可以选择一项：1.观看牌堆顶两张牌并获得其中一张；" ..
      "2.摸一张牌，然后下次执行选项1时多观看两张牌且多获得一张牌（至多修改三次）。",
  [":mini__jichong_inner"] = "当你受到伤害或回复体力后，你可以选择一项：1.观看牌堆顶{1}张牌并获得其中{2}张；" ..
      "2.摸一张牌，然后下次执行选项1时多观看两张牌且多获得一张牌（还可修改{3}次）。",
  [":mini__jichong_fin"] = "当你受到伤害或回复体力后，你可以选择一项：1.观看牌堆顶八张牌并获得其中四张；" ..
      "2.摸一张牌。",

  ["_mini__jichong"] = "修改次数",
  ["mini__jichong_view"] = "观看牌堆顶%arg张牌并获得其中%arg2张牌",
  ["mini__jichong_draw"] = "摸一张牌，然后下次执行选项1时多观看2张牌且多获得1张牌（还可修改%arg次）",
  ["mini__jichong_draw_fin"] = "摸一张牌",
  ["#mini__jichong-prey"] = "积宠：观看牌堆顶%arg张牌并获得其中%arg2张牌",
}

---@type TrigSkelSpec<DamageTrigFunc | RecoverTrigFunc>
local jichong_spec = {
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choices = { "mini__jichong_view:::" .. (2 * player:getMark("_mini__jichong_next") + 2) ..
        ":" .. player:getMark("_mini__jichong_next") + 1,
        player:getMark("_mini__jichong") < 3 and "mini__jichong_draw:::" .. (3 - player:getMark("_mini__jichong")) or "mini__jichong_draw_fin",
        "Cancel" }
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = self.name,
    })
    if choice ~= "Cancel" then
      event:setCostData(self, {choice = choice})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local choice = event:getCostData(self).choice ---@type string
    local room = player.room
    if choice:startsWith("mini__jichong_view") then
      local upgradeNum = player:getMark("_mini__jichong_next")
      room:setPlayerMark(player, "_mini__jichong_next", 0)
      local card = room:askToChooseCards(player, {
        target = player,
        min = upgradeNum + 1,
        max = upgradeNum + 1,
        flag = { card_data = { { "Top", room:getNCards(2 * upgradeNum + 2) } } },
        skill_name = self.name,
        prompt = "#mini__jichong-prey:::".. (2 * upgradeNum + 2) ..
        ":" .. upgradeNum + 1,
      })
      if #card > 0 then
        room:obtainCard(player, card, false, fk.ReasonPrey, player, self.name)
      end
    else
      player:drawCards(1, self.name)
      if player:isAlive() and player:getMark("_mini__jichong") < 3 then
        room:addPlayerMark(player, "_mini__jichong")
        room:addPlayerMark(player, "_mini__jichong_next")
      end
    end
  end
}

jichong:addEffect(fk.Damaged, jichong_spec)
jichong:addEffect(fk.HpRecover, jichong_spec)

jichong:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "_mini__jichong", 0)
  player.room:setPlayerMark(player, "_mini__jichong_next", 0)
end)

jichong:addAI({
  think_skill_invoke = Util.TrueFunc,
})

return jichong
